package com.ymatou.app.rec.service;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymt.base.YmatouBaseCall;
import com.ymttest.utils.EnvSetup;

public class GlProductCall extends YmatouBaseCall{
private static String opurl ="rec/jyh/glproduct/";
	
	public GlProductCall() {
		super(EnvSetup.getSystemUrl("glproduct"),opurl,"GET","PARAM");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Accept","text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
	
	/**
	 * 接口返回状态结果
	 * @return String OK
	 * */
	public String getStatus() {
		String ok = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			ok = jo.getString("status");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ok;
	}
	
	/**
	 * 获取接口返回数据的记录
	 * */
	public JSONArray getHits(){
		JSONArray list = null;
		try {
			JSONObject jsonsList = new JSONObject(getReturnData());
			list = jsonsList.getJSONArray("hits");
			/*if(list.toString().equals("[]")){
				return null;
			}*/
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	
	/**
	 * 得到接口公共返回值，如took，requestid，algorithm
	 * @return String 
	 * */
	public String getCommonInfo(String p) {
		String params = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			params = jo.getString(p);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return params;
	}
	
	/**
	 * 得到接口返回的boolean类型的参数
	 * @return boolean
	 * */
	public boolean getCommonInfoBoo(String p) {
		boolean paramBoolean = true;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			paramBoolean = jo.getBoolean(p);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramBoolean;
	}
	
	/**
	 * 获取接口返回总记录数
	 * @return int 
	 */
	public int getTotal(){
		int totals = 0;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			totals = jo.getInt("total");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return totals;
		
	}
	
	/**
	 * 得到searchInfo里的数据
	 * */
	public Object getSearchInfo(){
		Object list = null;
		try {
			JSONObject jsonsList = new JSONObject(getReturnData());
			list =  jsonsList.getJSONObject("searchInfo");
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
}
